/*
 * @Description: 
 * @Author: ljy
 * @Date: 2021-12-02 16:43:51
 * @LastEditors: ljy
 * @LastEditTime: 2021-12-07 22:40:53
 */
const mysql = require("mysql")
    // 1.创建连接池对象
let pool = mysql.createPool({
    connectionLimit: 10,
    host: "localhost",
    user: "root",
    password: "root2020",
    database: "mydb"
})
pool.myQuery = (sql, params, fun) => {
    // 2.获取连接对象
    pool.getConnection((err, conn) => {
        // 如果不出错，conn就是连接对象
        if (err) throw err;
        // 3.使用连接对象执行sql语句，并处理结果
        conn.query(sql, params, (error, result) => {
            if (error) throw error;
            // console.log(result);
            // 4.释放连接对象
            conn.release();
            fun(result)
                // 5.关闭连接池(一般不需要关闭连接池)
                // pool.end()
        })
    })
}

module.exports = pool